﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Framework;
using Contract.Domain;
using Framework.Domain;
namespace BaseService
{
    public abstract class BaseService<T> where T : Entity
    {
    
        #region Log
        /// <summary>
        /// 返回Id的T类型对象
        /// </summary>
        public virtual T FindById(object Id)
        {
            return (T)Dao.FindById(typeof(T), Id, null, null);
        }

     
        #endregion

        //private void RemoveMessage(string formId)
        //{
        //    msvc.DeleteMessage(formId);//将消息设为已读
        //}

        private Spring.Context.IApplicationContext _ctx;
        protected Spring.Context.IApplicationContext ctx
        {
            get
            {
                if (_ctx == null)
                    _ctx = Spring.Context.Support.ContextRegistry.GetContext();
                return _ctx;
            }
        }
       
        /// <summary>
        /// 返回记录集
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public System.Data.DataSet ExcuteDataSetByQueryInfo(QueryInfo info)
        {
            return Dao.ExecuteDataSet(info);
        }
        public int GetTotalCountByQueryInfo(QueryInfo info)
        {
            return Dao.GetTotalCount(info);
        }
        private Framework.IService.ICommonService _dao;
        public Framework.IService.ICommonService Dao
        {
            get
            {
                if (_dao == null)
                {
                    _dao = (Framework.IService.ICommonService)ctx["CommonService"];
                }
                return _dao;
            }
            set
            {
                _dao = value;
            }
        }



    }

    public static class ExtendHelper
    {
        public static string Join(this string[] array, string spr)
        {
            if (array == null || array.Length == 0)
                return "";

            StringBuilder builder = new StringBuilder();
            foreach (var item in array)
            {
                builder.AppendFormat("{0}{1}", item, spr);
            }

            return builder.ToString().Remove(builder.Length - spr.Length - 1);
        }

        public static string SqlIn(this string[] array)
        {
            if (array == null || array.Length == 0)
                return "";
            char left = '\'';
            char spr = ',';
            char right = '\'';

            StringBuilder builder = new StringBuilder();
            for (var idx = 0; idx < array.Length; idx++)//item in array)
            {
                if (idx < array.Length - 1)
                {
                    builder.AppendFormat("{0}{1}{2}{3}", left, array[idx], right, spr);// 如  'item',
                }
                else
                {
                    builder.AppendFormat("{0}{1}{2}", left, array[idx], right);// 如  'item'
                }
            }

            //builder = builder.Remove(builder.Length - 1, 1);

            return builder.ToString();
        }

        public static DateTime ParseToShortYmd(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day);
        }
     
        
    }
}
